import Qs from 'qs'
import request from '@/utils/request'
import { isNull } from '@/utils/tools'
import { upFile } from './file';

export function hkPost(url, data) {
  return request({
    url: url,
    method: 'post',
    data
  })
}

export function hkPut(url, data, isFromdata) {
  if (isNull(url)) return {};
  if (isFromdata) {
    return request({
      url: url,
      method: 'put',
      headers: {
        "Content-Type": "multipart/form-data",
        "X-Requested-With": "XMLHttpRequest",
      },
      data
    })
  }
  return request({
    url: url,
    method: 'put',
    data
  })
}

export function hkPutbody(url, data, isFromdata) {
  if (isNull(url)) return {};
  if (isFromdata) {
    return request({
      url: url,
      method: 'put',
      headers: {
        "Content-Type": "multipart/form-data",
        "X-Requested-With": "XMLHttpRequest",
      },
      data
    })
  }
  return request({
    url: url,
    method: 'put',
    data
  })
}

export function hkGet(url, data) {
  if (isNull(url)) return {};
  const query = hkGetRequestQurty(data);
  if (!isNull(query)) {
    url = url + "?"
  }
  return request({
    url: url + `${query}`,
    method: 'get',
  })
}

export function hkDel(url, data) {
  if (isNull(url)) return {};
  const query = hkGetRequestQurty(data);
  if (!isNull(query)) {
    url = url + "?"
  }
  return request({
    url: url + `${query}`,
    method: 'delete',
  })
}

export function hkUpload(file) {
  let param = new FormData(); //创建form对象
  param.append("img", file);
  return upFile(param);
}

export function hkGetRequestQurty(data) {
  let params = {};
  if (data !== null && data !== undefined) {
    for (let key in data) {
      let value = data[key];
      if (value !== null && value !== undefined && value !== "") {
        params[key] = value;
      }
    }
  }
  const query = Qs.stringify(params, {
    arrayFormat: 'brackets'
  })

  return query;
}


